package com.zmx.job.admin.controller.login;

import com.zmx.job.admin.controller.handler.ResponseResult;
import com.zmx.job.admin.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: 登录控制器
 * @ClassName: LoginController
 * @Author zhaomxr
 * @Date 2021-12-22 10:10
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("login")
    public String login(Model model, @RequestParam(required = true) String username, @RequestParam(required = true) String password) {
        // 校验用户名和密码
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            model.addAttribute("error", "请输入用户名和密码");
            return "login";
        }

        ResponseResult result = loginService.login(username, password);
        if (result.getCode() == 200) {
            model.addAttribute("msg", result.getMessage());
            return "home";
        } else {
            model.addAttribute("error", result.getMessage());
        }

        return "login";
    }
}
